<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ฟอร์มจัดการความขัดแย้งของข้อมูลเหตุการณ์ความไม่สงบ</title>
<script type="text/javascript">
   function update_event(recordid){
    var pmeters="";
    var evndte="",evnplcid="",evntme="",evndes="";
    evndte =  document.getElementById("evndte"+recordid).value;
	evnplcid =  document.getElementById("evnplcid"+recordid).value;
	evntme =  document.getElementById("evntme"+recordid).value;
    //alert(recordid+evndes);
	var urlstr="update_event.php";
	if (window.XMLHttpRequest) {
   		request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
   		request = new ActiveXObject("Microsoft.XMLHTTP");
	} 

	//var request = GXmlHttp.create();
	//urlstr = urlstr+"?recordid="+recordid+"&evndes="+evndes;
	pmeters="recordid="+recordid+"&evndte="+evndte+"&evnplcid="+evnplcid+"&evntme="+evntme;
	request.open('POST', urlstr , true); // request XML from PHP with AJAX call
	//HttPRequest.open('POST',url,true);             
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");           
	request.setRequestHeader("Content-length", pmeters.length);            
	request.setRequestHeader("Connection", "close"); 
	request.send(pmeters); 
	request.onreadystatechange = function () { // if function
		 		alert("test");
				alert(request.readyState);
		if (request.readyState == 4) {//if readyState == 4
			//var xmlDoc = request.responseXML;
			//message = xmlDoc.documentElement.getElementsByTagName("message");
			var textResponse=request.responseText; 
			//message = textResponse.documentElement.getElementsByTagName("message");
			alert("test send back");
			alert(textResponse);
			//markers = [];
			//alert(locations.length);
			//alert("markers len="+markers.length);
			//if (locations.length){
			//for (var i = 0; i < locations.length; i++) { // cycle thru locations
			//	markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")));
							// Add attributes to the marker so we can poll them later.
							// When clicked, an overlay will have these properties.
							//markers[i].infowindow = "This is "+locations[i].getAttribute("name");
							
							// Useful things to store on a marker (Not needed for this example, could be removed)
							// Tells you what index in the markers[] array an overlay is
							//markers[i].markerindex = i;
							// Store the location_id of the location the marker represents.
							// Very useful to know the true id of a marker, you could then make
							// AJAX calls to the database to update the information if you had it's location_id
							//markers[i].db_id = locations[i].getAttribute("location_id");
							//map.addOverlay(markers[i]);
						//}

					//}
		}// end if readyState == 4
	}// end function
	request.send(null); 
}// end function update_event

</script>
</head>
<body>
<form name="manage_event" id="manage_event">
  <?php
	include("connect/connect.php");
	mysql_select_db("dscc",$c);
	mysql_query("set names utf8");

	//$sql="select recordid, evnid, evndte, evnplcid, evntme, evndes, dsid, dspk from event";
	//เลือกเหตุการณ์ที่เกิดขึ้นในหมู่บ้านเดียวกันในวันเดียวกันแต่เวลาต่างก้ัน เพื่อนำมาตรวจสอบและยืนยัน
	$sql="select recordid, evnid, e1.evndte, e1.evnplcid, evntme, evndes, dsid, dspk from event  e1 inner join 
(select evndte,evnplcid from event group by 
evndte,evnplcid having count(*)>1) e2 on e1.evndte=e2.evndte and e1.evnplcid=e2.evnplcid
where recordid not in 

(select e1.recordid from event e1 inner join 
(select evndte,evnplcid,evntme from event group by 
evndte,evnplcid,evntme having count(*)>1) e2 on 
e1.evndte=e2.evndte and e1.evnplcid=e2.evnplcid and e1.evntme=e2.evntme)

order by evndte,evnplcid,evntme";
	$rs=mysql_query($sql);
	echo "<table width=800 border=1>";
	echo "\n<tr  valign=\"top\"><td>วันที่เกิดเหตุ</td><td>พื้นที่เกิดเหตุ</td><td>เวลา</td><td>รายละเอียด</td><td>แหล่งข้อมูล</td><td></td></tr>";
	while($rows = mysql_fetch_array($rs)){
		$recordid=$rows["recordid"];
		$evnid=$rows["evnid"];
		$evndte=$rows["evndte"];
		$evnplcid=$rows["evnplcid"];
		$evntme=$rows["evntme"];
		$evndes=$rows["evndes"];
		$dsid=$rows["dsid"];
		$dspk=$rows["dspk"];
		echo "\n<tr  valign=\"top\">";
		//echo "\n<td></td>";
		echo "\n<td><input type=\"hidden\" name=\"recordid$recordid\" id=\"recordid$recordid\" value=\"$recordid\"/>";
		echo "\n<input type=\"text\" name=\"evndte$recordid\" id=\"evndte$recordid\" value=\"$evndte\" size=8 maxlength=8/></td>";
		echo "\n<td><input type=\"text\" name=\"evnplcid$recordid\" id=\"evnplcid$recordid\" value=\"$evnplcid\" size=8 maxlength=8/></td>";
		echo "\n<td><input type=\"text\" name=\"evntme$recordid\" id=\"evntme$recordid\" value=\"$evntme\" size=4 maxlength=4/></td>";
		echo "\n<td><textarea name=\"evndes$recordid\" id=\"evndes$recordid\" cols=\"50\" rows=\"3\">$evndes</textarea></td>";
		//echo "\n<td>$dsid&nbsp;</td>";
		//echo "\n<td>$dspk&nbsp;</td>";
		echo "\n<td><div id=\"evnid$recordid\">$dsid</div></td>";
		echo "\n<td><input type=\"button\" name=\"btsave$recordid\" value=\"บันทึก\"/ onclick=\"javascript:update_event('$recordid');\"></td>";
		echo "</tr>";

	}
	echo "</table>";
?>
</form>
</body>
</html>
